package com.zhentao.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zhentao.mapper.ApUserLikeMapper;
import com.zhentao.mapper.ApUserSocialMapper;
import com.zhentao.model.common.dtos.ResponseResult;
import com.zhentao.model.common.enums.AppHttpCodeEnum;
import com.zhentao.model.social.dto.SocialLikeDto;
import com.zhentao.model.social.pojo.ApUserLike;
import com.zhentao.model.social.pojo.ApUserSocial;
import com.zhentao.model.social.vo.UserLikeVo;
import com.zhentao.model.user.pojo.ApUser;
import com.zhentao.model.social.pojo.ApUserCollect;
import com.zhentao.service.ApUserCollectService;
import com.zhentao.mapper.ApUserCollectMapper;
import com.zhentao.service.ApUserLikeService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
* @author 许文倩
* @description 针对表【ap_user_collect】的数据库操作Service实现
* @createDate 2024-07-08 15:04:01
*/
@Service
public class ApUserCollectServiceImpl extends ServiceImpl<ApUserCollectMapper, ApUserCollect>
    implements ApUserCollectService{
    @Resource
    ApUserCollectMapper apUserCollectMapper;
    @Resource
    ApUserSocialMapper apUserSocialMapper;
    @Resource
    ApUserLikeMapper apUserLikeMapper;

    @Override
    public ResponseResult getList(Integer userId) {
        List<ApUser> users=apUserCollectMapper.getList(userId);
        return ResponseResult.okResult(users);
    }

    @Override
    public ResponseResult scoialList(Integer userId) {
        List<Long> userIds = apUserCollectMapper.getLikeId(userId);
        if (userIds.isEmpty()){
            return  ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST,"没有关注记录");
        }
        List<SocialLikeDto> userLikeVos=apUserSocialMapper.findScoialByUserIds(userIds);
        for (SocialLikeDto likeDto:userLikeVos){
            QueryWrapper<ApUserLike> wrapper=new QueryWrapper<>();
            wrapper.lambda().eq(ApUserLike::getSocialId,likeDto.getId());
            wrapper.lambda().eq(ApUserLike::getLikeUserId,userId);
            ApUserLike apUserLike = apUserLikeMapper.selectOne(wrapper);
            if (apUserLike!=null){
                likeDto.setLikeStatus(1);
            }else {
                likeDto.setLikeStatus(0);
            }
            QueryWrapper<ApUserCollect> queryWrapper=new QueryWrapper<>();
            queryWrapper.lambda().eq(ApUserCollect::getUserId,userId);
            queryWrapper.lambda().eq(ApUserCollect::getLikeId,likeDto.getUserId());
            ApUserCollect apUserCollect = apUserCollectMapper.selectOne(queryWrapper);
            if (apUserCollect!=null){
                likeDto.setCollectStatus(1);
            }else {
                likeDto.setCollectStatus(0);
            }
        }
        return ResponseResult.okResult(userLikeVos);
    }

    @Override
    public ResponseResult delCollect(ApUserCollect apUserCollect) {
        QueryWrapper<ApUserCollect> wrapper=new QueryWrapper<>();
        wrapper.lambda().eq(ApUserCollect::getLikeId,apUserCollect.getLikeId());
        wrapper.lambda().eq(ApUserCollect::getUserId,apUserCollect.getUserId());
        ApUserCollect apUserCollect1 = apUserCollectMapper.selectOne(wrapper);
        apUserCollectMapper.deleteById(apUserCollect1.getId());
        return  ResponseResult.okResult("取消成功");
    }

    @Override
    public ResponseResult add(ApUserCollect apUserCollect) {
        apUserCollect.setCreatedTime(new Date());
        apUserCollectMapper.insert(apUserCollect);
        return ResponseResult.okResult("关注成功");
    }

    @Override
    public ResponseResult likeMe(Integer userId) {
       Integer count= apUserCollectMapper.likeMe(userId);
       return ResponseResult.okResult(count);
    }

    @Override
    public ResponseResult likeMeList(Integer userId) {
        List<Long> userIds = apUserCollectMapper.getUserId(userId);
        List<ApUser> apUsers= apUserCollectMapper.likeMeList(userIds);
        return ResponseResult.okResult(apUsers);
    }
}




